<script>
import common from '@/common/common';
export default {
	onLaunch: function() {
		// #ifdef H5
		this.seWxappid();
		// #endif
		console.log('App Launch');
	},
	onShow: function() {
		console.log('App Show');
	},
	onHide: function() {
		// console.log('App Hide');
	},
	methods: {
		// 公众号授权
		seWxappid() {
			let appid = 'wx111424c12a0e4d49';
			// let appid = 'wx1a3e2a30f11d6f46'; //测试账号
			let code = common.GetUrlParame('code');
			if (!code) {
				console.log('开始授权');
				const local = location.href;
				if (process.env.NODE_ENV === 'development') {
					console.log('开发环境');
					window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
						'&redirect_uri=' + encodeURIComponent(local) +
						'&response_type=code'+'&scope=snsapi_userinfo&state=STATE#wechat_redirect';
				} else {
					console.log('生产环境');
					window.location.href =
						'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
						appid +
						'&redirect_uri=' +
						encodeURIComponent(local) +
						'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
				}
			} else {
				this.getWechatUserInfo(code);
				// uni.setStorageSync('code',code);
			}
			try {
				console.log('appid******************未进入', appid);
			} catch (e) {
				console.log(e);
			}
		},
		/* ********************************************************* */
		// 公众号获取用户信息
		getWechatUserInfo(code) {
			var that = this;
			uni.request({
				url: '/app/wechatApi/getWechatUserInfo',
				method: 'POST',
				data: {
					code: code
				},
				header: {
					'Content-type': 'application/x-www-form-urlencoded'
				},
				success: res => {
					if (res.data.code == '200') {
						// console.log(res.data.data);
						uni.setStorageSync('openid', res.data.data.openId);
						uni.setStorageSync('name', res.data.data.name);
						uni.setStorageSync('headImg', res.data.data.headimgurl);
						uni.setStorageSync('address', res.data.data.address);
						// 转派人员的id
						uni.setStorageSync('deptId', res.data.data.deptId);
						// 登录人员的身份
						for(let i=0 ;i<res.data.data.roleList.length;i++){
							if(res.data.data.roleList[i] == "1207491780946202626"){
								uni.setStorageSync('nowType','2')
							}else{
								uni.setStorageSync('nowType','1')
							}
						}
						
						if (res.data.data.account == '') {
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}else{
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						});
					}
				},
				error: e => {
					console.log(e);
				}
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background: #ffffff;
}
/*每个页面公共css */
@import './common/uni.css';
@import './components/uParse/src/wxParse.css';
.uni-btn{
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.uni-img-icon{
	width: 30upx;
	height: 30upx;
}

/* #ifdef MP */
/* 强制小程序按钮type为primary时颜色为蓝色 */
button[type='primary'] {
	background-color: #007aff !important;
}
button[type='primary'][plain] {
	border-color: #007aff !important;
	color: #007aff !important;
	background-color: transparent !important;
}
/* #endif */

/* #ifdef H5 */
@supports (bottom: constant(safe-area-inset-bottom)) or (bottom: env(safe-area-inset-bottom)) {
	uni-tabbar {
		bottom: constant(safe-area-inset-bottom) !important;
		bottom: env(safe-area-inset-bottom) !important;
	}
}
/* #endif */
</style>
